/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.TipoDocumento;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;
/**
 *
 * @author roderick
 */
public class TipoDocumentoModel extends DataBase{

    private int id;
    private String tdcdesc;
    private String tdcabr;

    public TipoDocumentoModel(){

    }

    private boolean validar(){
        return true;

    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("insert into tipodocumento(tdcdesc,tdcabr) values(?,?);");
                stmt.setString(1,this.tdcdesc);
                stmt.setString(2,this.tdcabr);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("update tipodocumento set tdcdesc = ? ,tdcabr = ? where tdccod = ? ;");
                stmt.setString(1,this.tdcdesc);
                stmt.setString(2,this.tdcabr);
                stmt.setInt(3, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from tipodocumento where tdccod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT * FROM tipodocumento order by tdcdesc ");
    }


    public ResultSet finder(String var){
        return this.find("SELECT tdccod, tdcdesc, tdcabr FROM tipodocumento WHERE tdcdesc LIKE '"+var+"%' order by tdcdesc;");
    }

    public ResultSet finder(int var){
        return this.find("SELECT tdccod, tdcdesc, tdcabr FROM tipodocumento WHERE tdccod = "+var+";");
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String gettdcabr() {
        return tdcabr;
    }

    public void settdcabr(String tdcabr) {
        this.tdcabr = tdcabr;
    }

    public String gettdcdesc() {
        return tdcdesc;
    }

    public void settdcdesc(String tdcdesc) {
        this.tdcdesc = tdcdesc;
    }

}
